package itbour.onetouchshow.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import itbour.onetouchshow.R;
import itbour.onetouchshow.utils.UIUtils;
import itbour.onetouchshow.widget.GlideRoundTransform;

/**
 * Created by zjl_d on 2017/12/18.
 */

public class InchingChangeTabColorAdapter extends RecyclerView.Adapter {

    public String type = "";
    public int[] yuanshiColor = {0, 0, 0};
    public int nowSelect = -1;
    InchingChangeTextColorAdapter.OnTextColorItemClick onTextColorItemClick;
    private Context context;
    private List<List<Integer>> colors;


    public InchingChangeTabColorAdapter(Context context, List<List<Integer>> colors) {
        this.context = context;
        this.colors = colors;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new InchingChangeTextColorAdapter.ItemViewHolder(View.inflate(parent.getContext(), R.layout.adapter_inching_change_text_color, null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder outHolder, final int position) {
        final InchingChangeTextColorAdapter.ItemViewHolder holder = (InchingChangeTextColorAdapter.ItemViewHolder) outHolder;
        List<Integer> integers = colors.get(position);
        final Integer r = integers.get(0);
        final Integer g = integers.get(1);
        final Integer b = integers.get(2);


        RelativeLayout.LayoutParams layoutParamsRl = (RelativeLayout.LayoutParams) holder.rl_base_color.getLayoutParams();
        holder.rl_base_color.setLayoutParams(layoutParamsRl);


        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) holder.iv_base_color.getLayoutParams();

        layoutParams.width = (UIUtils.WHD()[0] - UIUtils.dp2px(144)) / 6;
        layoutParams.height = layoutParams.width;
        holder.iv_base_color.setLayoutParams(layoutParams);


        RelativeLayout.LayoutParams broadLp = (RelativeLayout.LayoutParams) holder.iv_base_color_bord.getLayoutParams();
        broadLp.width = layoutParams.width + UIUtils.dp2px(4);
        broadLp.height = layoutParams.height + UIUtils.dp2px(2);
        broadLp.setMargins(UIUtils.dp2px(10), UIUtils.dp2px(10), UIUtils.dp2px(10), UIUtils.dp2px(10));

        holder.iv_base_color_bord.setLayoutParams(broadLp);


        if (nowSelect == position) {
            holder.iv_base_color_bord.setVisibility(View.VISIBLE);
        } else {

            holder.iv_base_color_bord.setVisibility(View.INVISIBLE);
        }

        Glide.with(context).load(R.drawable.iv_white_v56)
                .apply(new RequestOptions().transform(new GlideRoundTransform(context)))
                .into(holder.iv_base_color);
        holder.iv_base_color.setColorFilter(Color.rgb(r, g, b));

        holder.iv_base_color.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int oldSelect = nowSelect;
                nowSelect = position;
                InchingChangeTabColorAdapter.this.notifyItemChanged(position);
                InchingChangeTabColorAdapter.this.notifyItemChanged(oldSelect);

                if (onTextColorItemClick != null) {
                    onTextColorItemClick.onTextColorClick(r, g, b, type);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return colors == null ? 0 : colors.size();
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setOnTextColorItemClick(InchingChangeTextColorAdapter.OnTextColorItemClick onTextColorItemClick) {
        this.onTextColorItemClick = onTextColorItemClick;
    }

    public interface OnTextColorItemClick {
        void onTextColorClick(int r, int g, int b, String type);
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        public RelativeLayout rl_base_color;
        public ImageView iv_base_color;
        public ImageView iv_base_color_bord;

        public ItemViewHolder(View itemView) {
            super(itemView);
            rl_base_color = itemView.findViewById(R.id.rl_base_color);
            iv_base_color = itemView.findViewById(R.id.iv_base_color);
            iv_base_color_bord = itemView.findViewById(R.id.iv_base_color_bord);

        }
    }
}
